package com.base.cn.platform.os.controller.web.classes.member.task;

import com.base.cn.platform.os.common.WebBaseController;
import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.exam.paper.ExamPaper;
import com.base.cn.platform.os.entity.exam.record.ExamRecord;
import com.base.cn.platform.os.service.exam.record.ExamRecordService;
import com.base.cn.platform.os.service.exam.record.LoadStartExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 我的作业
 * @author ll
 * @date 2018/7/12 8:50
 */
@Controller
@RequestMapping("/web/uc/task")
public class WebUserClassesTaskController extends WebBaseController {

    @Autowired
    private ExamRecordService examRecordService;
    @Autowired
    private LoadStartExamService loadStartExamService;

    /**
     * 获取用户作业
     *
     * @return
     */
    @RequestMapping("/getClassesTaskList")
    public String getClassesTaskList(){
        return resultPage("/classes/personal/task/task-list");
    }

    /**
     * 获取用户作业
     * @param request HttpServletRequest
     * @param model Model
     * @param condition 查询条件对象
     * @param currentPage 页码
     * @return 数据页面
     */
    @RequestMapping("/ajax/getAjaxClassesTaskList")
    public String getAjaxClassesTaskList(HttpServletRequest request, Model model,
                                     @RequestBody Map<String,Object> condition,
                                     @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        condition.put("userId",userId);
        condition.put("useType",2);
        condition.put("examPaperData",true);
        condition.put("courseData",true);
        Map<String,Object> dataMap = examRecordService.findUserTaskExamRecordListPage(condition,currentPage,6);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/classes/personal/task/ajax-task-list");
    }

    /**
     * 作业详情
     *
     * @param personalTaskId 作业Id
     * @return
     */
    @RequestMapping("/getClassesTaskInfo")
    public String getClassesTaskInfo(Model model,
                                     @RequestParam("personalTaskId") BigDecimal personalTaskId) {
        Map<String,Object> condition = new HashMap<>();
        condition.put("id",personalTaskId);
        condition.put("examPaperData",true);
        condition.put("courseData",true);
        ExamRecord examRecord = examRecordService.findExamRecordById(condition);
        model.addAttribute("examRecord",examRecord);
        int stateType = 1;//默认超提交
        //最后提交时间
        String strUpdateTime =DateUtils.format (examRecord.getUpdateTime(),"yyyy-MM-dd HH:mm:ss");
        //试卷对象
        ExamPaper examPaper = examRecord.getExamPaper();

        if(ObjectUtils.isEmpty(examPaper.getEndTime())){
            stateType = 2;//正常提交
        }else{
            //最晚提交时间
            String strEndTime =DateUtils.format (examPaper.getEndTime(),"yyyy-MM-dd HH:mm:ss");
            //试卷最后提交时间
            Date updateTime = DateUtils.parseDate(strUpdateTime,"yyyy-MM-dd HH:mm:ss");
            //规定最晚提交时间
            Date endTime = DateUtils.parseDate(strEndTime,"yyyy-MM-dd HH:mm:ss");
            if(endTime.after(updateTime)){
                stateType = 2;//正常提交
            }
        }
        model.addAttribute("stateType",stateType);
        return resultPage("/classes/personal/task/task-info");
    }


    /**
     * 去做作业页面
     * @param model Model
     * @param request HttpServletRequest
     * @param id 作业记录ID
     * @return
     */
    @RequestMapping("/startTask")
    public String doTheHomework(Model model,HttpServletRequest request,@RequestParam("id") BigDecimal id) {
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String,Object> condition = new HashMap<>();
        condition.put("id",id);
        condition.put("userId",userId);
        Map<String,Object> dataMap = loadStartExamService.loadExamRecord(condition);
        if(ResultUtil.getSuccess(dataMap)){
            model.addAttribute("examRecord",ResultUtil.getEntity(dataMap));
        }
        return resultPage("/classes/personal/task/start-task");
    }

    //====================================================================================

    /**
     * 加载作业试题
     * @param request HttpServletRequest
     * @param model Model
     * @param examRecordId 考试记录ID
     * @param status 状态，1未完成，2已完成
     * @param currentPage 页码
     * @return 数据列表
     */
    @RequestMapping("/loadTaskQuestionList")
    public String loadTaskQuestionList(HttpServletRequest request,
                                       Model model,
                                       @RequestParam("examRecordId") BigDecimal examRecordId,
                                       @RequestParam("status") int status,
                                       @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String,Object> params = new HashMap<>();
        params.put("type",1);
        params.put("userId",userId);
        params.put("examRecordId",examRecordId);
        params.put("questionData",true);
        params.put("templateData",true);
        params.put("loreSetPointData",true);
        Map<String,Object> dataMap = loadStartExamService.loadExamQuestionRecord(params,currentPage,10);
        model.addAttribute("dataMap",dataMap);
        model.addAttribute("status",status);
        return resultPage("/classes/personal/task/task-question-list");
    }

    /**
     * 加载作业答题卡
     * @param request
     * @param model
     * @param examRecordId
     * @return
     */
    @RequestMapping("/loadTaskCard")
    public String loadTaskCard(HttpServletRequest request,
                               Model model,
                               @RequestParam("examRecordId") BigDecimal examRecordId){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        int pageSize = 10;
        Map<String,Object> cardMap = loadStartExamService.loadExamRecordCard(userId,examRecordId,pageSize);
        model.addAttribute("cardMap",cardMap);
        return resultPage("/classes/personal/task/task-card-list");
    }
}
